Option Strict On
Option Explicit On

'Copyright (c) 2007-2010, William Severance, Jr., WESNet Designs
'All rights reserved.

'Redistribution and use in source and binary forms, with or without modification, are permitted
'provided that the following conditions are met:

'Redistributions of source code must retain the above copyright notice, this list of conditions
'and the following disclaimer.

'Redistributions in binary form must reproduce the above copyright notice, this list of conditions
'and the following disclaimer in the documentation and/or other materials provided with the distribution.

'Neither the name of William Severance, Jr. or WESNet Designs may be used to endorse or promote
'products derived from this software without specific prior written permission.

'Disclaimer: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
'            OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
'            AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE
'            FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
'            LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
'            INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
'            OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
'            IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

'Although I will try to answer questions regarding the installation and use of this software when
'such questions are submitted via e-mail to the below address, no promise of further
'support or enhancement is made nor should be assumed.

'Developer Contact Information:
'     William Severance, Jr.
'     WESNet Designs
'     679 Upper Ridge Road
'     Bridgton, ME 04009
'     Phone: 207-647-9375
'     E-Mail: bill@wesnetdesigns.com
'     Website: www.wesnetdesigns.com

Imports DotNetNuke
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Entities.Tabs


Namespace WESNet.DNN.Modules.EPrayer
    Public Class Stats

#Region "Private Members"
        Private _PortalId As Integer = Null.NullInteger
        Private _TabId As Integer = Null.NullInteger
        Private _TabName As String = String.Empty
        Private _ModuleId As Integer = Null.NullInteger
        Private _ModuleTitle As String = String.Empty

        Private _Requests As Integer = 0
        Private _Updates As Integer = 0
        Private _MostRecentRequestID As Integer = Null.NullInteger
        Private _MostRecentPostID As Integer = Null.NullInteger
        Private _MostRecentAuthorID As Integer = Null.NullInteger

        Private _MostRecentPostDate As DateTime = Null.NullDate
        Private _PostsToModerate As Integer = 0

        Private _MostRecentAuthor As String = String.Empty
#End Region

#Region "Public Properties"

        Public ReadOnly Property PortalId() As Integer
            Get
                If _PortalId = Null.NullInteger Then
                    _PortalId = DotNetNuke.Entities.Portals.PortalController.GetCurrentPortalSettings.PortalId
                End If
                Return _PortalId
            End Get
        End Property

        Public ReadOnly Property ModuleId() As Integer
            Get
                Return _ModuleId
            End Get
        End Property

        Public ReadOnly Property ModuleTitle() As String
            Get
                If _ModuleTitle = String.Empty Then
                    Dim mc As New ModuleController
                    _ModuleTitle = mc.GetModule(_ModuleId, _TabId).ModuleTitle
                End If
                Return _ModuleTitle
            End Get
        End Property

        Public ReadOnly Property TabId() As Integer
            Get
                Return _TabId
            End Get
        End Property

        Public ReadOnly Property TabName() As String
            Get
                If _TabName = String.Empty Then
                    Dim tc As New TabController
                    _TabName = tc.GetTab(_TabId, PortalId, False).TabName
                End If
                Return _TabName
            End Get
        End Property

        Public Property Requests() As Integer
            Get
                Return _Requests
            End Get
            Set(ByVal value As Integer)
                _Requests = value
            End Set
        End Property

        Public Property Updates() As Integer
            Get
                Return _Updates
            End Get
            Set(ByVal value As Integer)
                _Updates = value
            End Set
        End Property

        Public Property MostRecentRequestID() As Integer
            Get
                Return _MostRecentRequestID
            End Get
            Set(ByVal value As Integer)
                _MostRecentRequestID = value
            End Set
        End Property

        Public Property MostRecentPostID() As Integer
            Get
                Return _MostRecentPostID
            End Get
            Set(ByVal value As Integer)
                _MostRecentPostID = value
            End Set
        End Property

        Public Property MostRecentAuthorID() As Integer
            Get
                Return _MostRecentAuthorID
            End Get
            Set(ByVal value As Integer)
                _MostRecentAuthorID = value
            End Set
        End Property

        Public Property MostRecentPostDate() As DateTime
            Get
                Return _MostRecentPostDate
            End Get
            Set(ByVal value As DateTime)
                _MostRecentPostDate = value
            End Set
        End Property

        Public Property PostsToModerate() As Integer
            Get
                Return _PostsToModerate
            End Get
            Set(ByVal value As Integer)
                _PostsToModerate = value
            End Set
        End Property

        Public ReadOnly Property MostRecentAuthor() As String
            Get
                If _MostRecentAuthor = String.Empty Then
                    If _MostRecentAuthorID <> Null.NullInteger Then
                        Dim epc As New EPrayerController
                        Dim AuthorInfo As UserInfo = epc.GetUser(_ModuleId, _MostRecentAuthorID)
                        If Not AuthorInfo Is Nothing Then
                            Select Case AuthorInfo.IdentifyBy
                                Case 0
                                    _MostRecentAuthor = AuthorInfo.Username
                                Case 1
                                    _MostRecentAuthor = AuthorInfo.DisplayName
                                Case 2
                                    _MostRecentAuthor = AuthorInfo.Alias
                            End Select
                        End If
                    End If
                End If
                Return _MostRecentAuthor
            End Get
        End Property
#End Region

#Region "Constructor"

        Public Sub New()

        End Sub

        Public Sub New(ByVal ModuleId As Integer, ByVal TabId As Integer)
            _ModuleId = ModuleId
            _TabId = TabId
        End Sub

        Public Sub New(ByVal ModuleId As Integer, ByVal TabId As Integer, ByVal RefreshStats As Boolean)
            _ModuleId = ModuleId
            _TabId = TabId
            If RefreshStats Then Refresh()
        End Sub

        Public Sub New(ByVal ModuleId As Integer, ByVal TabId As Integer, ByVal dr As IDataReader)
            _ModuleId = ModuleId
            _TabId = TabId
            _Requests = Null.SetNullInteger(dr.Item("Requests"))
            _Updates = Null.SetNullInteger(dr.Item("Updates"))
            _MostRecentRequestID = Null.SetNullInteger(dr.Item("MostRecentRequestID"))
            _MostRecentPostID = Null.SetNullInteger(dr.Item("MostRecentPostID"))
            _MostRecentAuthorID = Null.SetNullInteger(dr.Item("MostRecentAuthorID"))
            _MostRecentPostDate = Null.SetNullDateTime(dr.Item("MostRecentPostDate"))
            _PostsToModerate = Null.SetNullInteger(dr.Item("PostsToModerate"))
        End Sub

#End Region

#Region "Public/Private Methods"

        Public Overloads Sub Reload()
            Reload(False)
        End Sub

        Public Overloads Sub ReLoad(ByVal Refresh As Boolean)
            Dim epc As New EPrayerController
            If Refresh Then epc.RefreshStats(_ModuleId)
            Dim tmpStats As Stats = epc.GetStats(_ModuleId)
            With tmpStats
                _Requests = .Requests
                _Updates = .Updates
                _MostRecentRequestID = .MostRecentRequestID
                _MostRecentPostID = .MostRecentPostID
                _MostRecentAuthorID = .MostRecentAuthorID
                _MostRecentPostDate = .MostRecentPostDate
                _PostsToModerate = .PostsToModerate
            End With
        End Sub

        Public Sub Refresh()
            Dim epc As New EPrayerController
            epc.RefreshStats(_ModuleId)
        End Sub

        Public Sub Update()
            Dim epc As New EPrayerController
            epc.UpdateStats(ModuleId, Requests, Updates, MostRecentRequestID, MostRecentPostID, MostRecentAuthorID, MostRecentPostDate, PostsToModerate)
        End Sub
#End Region

#Region "Shared Public Method"
        Public Shared Sub Initialize(ByVal ModuleId As Integer)
            Dim epc As New EPrayerController
            epc.InitializeStats(ModuleId)
        End Sub
#End Region

    End Class
End Namespace
